const query = require('./fun')
const sqlCount = 'SQL_CALC_FOUND_ROWS';
const { TWOTYPE, USER, ONETYPE, GOODS, ORDERS } = require('./database')
let { sqlUpdateStr, sqlAddStr, sqlSelectStr, sqlWhereStr } = require('../util.js');
/**
 * 所有请求方法
 */
/**
 * 判断是否存在注册用户
 */
function isUser(data) {
    return new Promise((resolve, reject) => {
        query(`select  * from ${USER} where username=? `, data)
            .then((res) => {
                resolve(res)
            })
            .catch((err) => {
                reject(reject)
            })
    })
}
/**
 * 注册用户
 */
function registerUser(data) {
    return new Promise((resolve, reject) => {
        let obj = sqlAddStr(data);
        query(`INSERT INTO ${USER}(${obj.keys}) VALUES(${obj.vals}) `)
            .then((res) => {
                resolve(res)
            })
            .catch((err) => {
                reject(reject)
            })
    })
}
/**
 *  登录
 */
function isLogin(data) {
    return new Promise((resolve, reject) => {
        query(`select  * from ${USER} where username=? and password=?`, data)
            .then((res) => {
                resolve(res)
            })
            .catch((err) => {
                reject(reject)
            })
    })
}
/**
 * 获取商品一级分类
 */
function getOneType() {
    return new Promise((resolve, reject) => {
        query(`select * from ${ONETYPE}`)
            .then((res) => {
                resolve(res)
            })
            .catch((err) => {
                reject(reject)
            })
    })
}
/**
 * 获取商品二级分类
 */
function getTwoType() {
    return new Promise((resolve, reject) => {
        query(`select * from ${TWOTYPE}`)
            .then((res) => {
                resolve(res)
            })
            .catch((err) => {
                reject(reject)
            })
    })
}
/**
 * 根据二级分类获取商品
 */
function getGoodsByTwo(twoTypeId) {
    return new Promise((resolve, reject) => {
        query(`select * from ${GOODS} where twoTypeId=?`, { twoTypeId })
            .then((res) => {
                resolve(res)
            })
            .catch((err) => {
                reject(reject)
            })
    })
}
/**
 * 查看商品详情
 */
function getGoodsById(goodsId) {
    return new Promise((resolve, reject) => {
        query(`select * from ${GOODS} WHERE ${GOODS}.goodsId=?`, { goodsId })
            .then((res) => {
                resolve(res[0])
            })
            .catch((err) => {
                reject(reject)
            })
    })
}
/**
 * 添加订单
 */
function submitOrder(data) {
    return new Promise((resolve, reject) => {
        let obj = sqlAddStr(data);
        query(`INSERT INTO ${ORDERS}(${obj.keys}) VALUES(${obj.vals}) `)
            .then((res) => {
                resolve(res)
            })
            .catch((err) => {
                reject(reject)
            })
    })
}
/**
 * 模糊查询商品
 */
function getGoodsByName(like) {
    return new Promise((resolve, reject) => {
        let condition = '';
        condition = sqlSelectStr(condition, like);
        query(`select ${sqlCount} * from ${GOODS} ${condition}`, {}, true)
            .then(async (res) => {
                resolve(res);
            })
            .catch((err) => {
                reject(err)
            })
    })
}
exports.registerUser = registerUser;
exports.isUser = isUser;
exports.isLogin = isLogin;
exports.getOneType = getOneType;
exports.getTwoType = getTwoType;
exports.getGoodsByTwo = getGoodsByTwo;
exports.getGoodsById = getGoodsById;
exports.submitOrder = submitOrder;
exports.getGoodsByName=getGoodsByName;
